package com.edu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author 75099637@qq.com
 * @PackageName SpringMybatis
 * @Package com.springMybatis.aspect
 * @Date 2023/11/5 12:49
 * @Version 1.0
 */
public class MyAspect {

    public void before(JoinPoint jt){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
        //需要一个字符串变量来接收
        String format = sdf.format(date);
        System.out.println("方法开始执行与："+format);
    }

    public void afterReturn(JoinPoint jt,Object result){
        System.out.println("后置返回："+result);
    }

    public void afterThrowing(JoinPoint jt,Throwable e){
        System.out.println("抛出异常："+e.getClass());
    }

    public void after(JoinPoint jt){
        System.out.println("执行了后置通知");
    }

    public Object around(ProceedingJoinPoint jt) throws Throwable {
        // 记录执行时间
        Date startTime = new Date();
        // 执行登录操作
        Object result = jt.proceed();
        // 计算执行时间
        Date endTime = new Date();
        long duration = endTime.getTime() - startTime.getTime();
        System.out.println("耗时：" + duration + "ms");
        return result;
    }
}
